package com.suyafei.controller;


import com.suyafei.enums.BizCodeEnum;
import com.suyafei.request.LockCouponRecordRequest;
import com.suyafei.service.CouponRecordService;
import com.suyafei.util.JsonData;
import com.suyafei.vo.CouponRecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author suyafei
 * @since 2021-03-03
 */
@Api(tags = "优惠券记录模块")
@RestController
@RequestMapping("/api/coupon_record/v1")
public class CouponRecordController {

    @Autowired
    private CouponRecordService couponRecordService;
    @ApiOperation("分页查询个人优惠券")
    @GetMapping("page")
    public JsonData page(@ApiParam("当前页") @RequestParam(value = "page", defaultValue = "1") int page,
                         @ApiParam("每页多少") @RequestParam(value = "size", defaultValue = "20") int size) {


        Map<String, Object> pageMap = couponRecordService.page(page, size);

        return JsonData.buildSuccess(pageMap);
    }

    @ApiOperation("查询优惠券记录详情")
    @GetMapping("detail/{record_id}")
    public JsonData getCouponRecordDetail(@PathVariable("record_id") long recordId) {
        CouponRecordVO couponRecordVO = couponRecordService.findById(recordId);
        return couponRecordVO == null ? JsonData.buildSuccess(BizCodeEnum.COUPON_NO_EXITS.getMessage()) : JsonData.buildSuccess(couponRecordVO);
    }

    @ApiOperation("rpc-锁定，优惠券记录")
    @PostMapping("lock_records")
    public JsonData lockCouponRecords(@ApiParam("锁定优惠券请求对象") @RequestBody LockCouponRecordRequest lockCouponRecordRequest) {

        JsonData jsonData = couponRecordService.lockCouponRecords(lockCouponRecordRequest);

        return jsonData;


    }

}

